package cn.bubi.sdk.starter;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public enum ErrorCode{

    SUCCESS(0, "成功"),
    //1000 - 2000 BlockChain 使用
    ACCOUNT_CREATE_ERROR(1001, "创建账号失败"),
    ACCOUNT_NOTFOUND(1002, "未查询到相关账号"),
    ASSET_ISSUE_ERROR(1021, "发行资产失败"),
    ASSET_TRANSFER_ERROR(1022, "转移资产失败"),
    METADATA_UPDATE_ERROR(1041, "元数据更改失败"),
    METADATA_NOTFOUND(1042, "元数据未查询到"),
    TRANSACTION_NOT_FOUND(1061, "未查询到相关交易"),
    CONTACT_INVOKE_ERROR(1081, "调用自动合约失败"),
    ERROR(99999, "内部错误"),;

    private int code;

    private String description;

    private static Map<Integer, ErrorCode> maps = new HashMap<>();

    static{
        for (ErrorCode error : ErrorCode.values()) {
            maps.put(error.getCode(), error);
        }
    }

    private ErrorCode(int code, String desc){
        this.code = code;
        this.description = desc;
    }

    public int getCode(){
        return code;
    }

    public String getDescription(){
        return description;
    }

    public static boolean isError(int code){
        return code != SUCCESS.code && maps.containsKey(code);
    }

    public String toString() {
        return JSON.toJSONString(this, true);
    }

    public static String getDescription(int code){
        ErrorCode error = maps.get(code);
        if (error == null) {
            return null;
        }
        return error.getDescription();
    }
}
